// Copyright 2016-2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Copyright 2024, Black Sesame Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TYPE_SUPPORT_COMMON_HPP_
#define TYPE_SUPPORT_COMMON_HPP_

#include <sstream>
#include <string>

#include "dal_msg/error_handling.h"

#include "dal_fastrtps_shared_cpp/TypeSupport.hpp"

#include "dal_fastrtps_cpp/MessageTypeSupport.hpp"
#include "dal_fastrtps_cpp/ServiceTypeSupport.hpp"

#include "dal_fastrtps_cpp/identifier.hpp"

#include "dalidl_typesupport_fastrtps_c/identifier.h"
#include "dalidl_typesupport_fastrtps_cpp/identifier.hpp"
#include "dalidl_typesupport_fastrtps_cpp/message_type_support.h"
#include "dalidl_typesupport_fastrtps_cpp/service_type_support.h"
#define DAL_FASTRTPS_CPP_TYPESUPPORT_C dalidl_typesupport_fastrtps_c__identifier
#define DAL_FASTRTPS_CPP_TYPESUPPORT_CPP dalidl_typesupport_fastrtps_cpp::typesupport_identifier

using MessageTypeSupport_cpp = dal_fastrtps_cpp::MessageTypeSupport;
using TypeSupport_cpp = dal_fastrtps_cpp::TypeSupport;
using RequestTypeSupport_cpp = dal_fastrtps_cpp::RequestTypeSupport;
using ResponseTypeSupport_cpp = dal_fastrtps_cpp::ResponseTypeSupport;

inline std::string _create_type_name(const message_type_support_callbacks_t* members)
{
    if (!members) {
        DAL_SET_ERROR_MSG("members handle is null");
        return "";
    }

    std::ostringstream ss;
    std::string message_namespace(members->message_namespace_);
    std::string message_name(members->message_name_);

    // Compatible with cyberRT.
    const char* env_enable = getenv("CYBER_DAL_ENABLE");
    const char* env_msg_type = getenv("CYBER_DAL_MSG_TYPE");
    if (message_namespace == "dal::msg" && message_name == "UnderlayMessage" &&
        env_enable && strcmp(env_enable, "true") == 0 &&
        env_msg_type && strcmp(env_msg_type, "UnderlayMessage") == 0) {
        return message_name;
    }

    if (!message_namespace.empty()) {
        ss << message_namespace << "::";
    }
    ss << "dds_::" << message_name << "_";
    return ss.str();
}

#endif  // TYPE_SUPPORT_COMMON_HPP_
